﻿using Autofac;
using Autofac.Integration.Mvc;
using AutoMapper;
using Owin;
using System.Web.Mvc;
using Yz.DataAccess.DbAccessses;
using Yz.DataAccess.DbAccessses.Interface;
using Yz.DataAccess.ModelMapper;
using Yz.Mvc.Startups;

namespace Yz.Manage
{
    public static partial class StartupExtension
    {
        public static void RegisterAutofac(this IAppBuilder app)
        {
            //初始化定时任务
            //创建autofac管理注册类的容器实例
            var builder = StartupAutofac.RegisterService();

            builder.Register<IConfigurationProvider>(ctx => new MapperConfiguration(x =>
            {
                x.AddProfile<SourceProfileApi>();
                x.AddProfile<SourceProfileVm>();
            })).PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies).InstancePerLifetimeScope();
            #region 数据库初始化注入
            builder.RegisterType<YzInitializer>()
         .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies).As<IYzInitializer>()
         .InstancePerLifetimeScope();
            #endregion

            //builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired(); //生成具体的实例 
            builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired(); ////// 使用这一种注入controller或者使用上边的这一种（Yz.Mvc父类也要注入）
            var container = builder.Build();
            app.UseAutofacMiddleware(container);
            app.UseAutofacMvc();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}